package com.food.user.hander;


import com.food.user.commos.Result;

import com.food.user.exception.MyParameterException;
import com.food.user.exception.commentInfoException.CommentInfoException;
import com.food.user.exception.dietTimeConfigException.DietTimeConfigException;
import com.food.user.exception.foodInfoException.FoodInfoException;
import com.food.user.exception.productsTypeException.ProductsTypeException;
import com.food.user.exception.storeInfoException.StoreInfoException;
import com.food.user.exception.storeRegisterException.StoreRegisterException;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.MalformedJwtException;
import io.jsonwebtoken.security.SignatureException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


@RestControllerAdvice
public class GlobleExceptionHander {

    //评价信息异常处理
    @ExceptionHandler(CommentInfoException.class)
    public Result handleProductsException(CommentInfoException e){
        e.printStackTrace();
        return Result.fail(e.getCode(),e.getMessage());
    }


    //菜品信息异常处理
    @ExceptionHandler(FoodInfoException.class)
    public Result handleProductsException(FoodInfoException e){
        e.printStackTrace();
        return Result.fail(e.getCode(),e.getMessage());
    }


    //餐品类型异常处理
    @ExceptionHandler(ProductsTypeException.class)
    public Result handleProductsException(ProductsTypeException e){
        e.printStackTrace();
        return Result.fail(e.getCode(),e.getMessage());
    }


    @ExceptionHandler({MalformedJwtException.class, SignatureException.class})
    public Result handleJwtException(Exception e){
        e.printStackTrace();
        return Result.fail(500,"token无效");
    }



    //参数校验异常处理
    @ExceptionHandler(MyParameterException.class)
    public Result handleParameterException(MyParameterException e){
        e.printStackTrace();
        return Result.fail(e.getCode(),e.getMessage());
    }

    @ExceptionHandler({ExpiredJwtException.class})
    public Result handleJExpiredJwtxception(Exception e){
        e.printStackTrace();
        return Result.fail(500,"token已过期请重新登录");
    }

    //商铺注册异常处理
    @ExceptionHandler(StoreRegisterException.class)
    public Result handleStoreRegisterException(StoreRegisterException e){
        e.printStackTrace();
        return Result.fail(e.getCode(),e.getMessage());
    }


    //商铺info异常处理
    @ExceptionHandler(StoreInfoException.class)
    public Result handleStoreInfoException(StoreInfoException e){
        e.printStackTrace();
        return Result.fail(e.getCode(),e.getMessage());
    }

    //商铺用餐时间配置表异常处理
    @ExceptionHandler(DietTimeConfigException.class)
    public Result handleDietTimeConfigException(DietTimeConfigException e){
        e.printStackTrace();
        return Result.fail(e.getCode(),e.getMessage());
    }
    //兜底
    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e) {
        e.printStackTrace();
        return Result.fail(500,e.getMessage());
    }
}